<?php

namespace App\Admin\Controllers;

use App\Broker;
use App\BrokerCall;
use App\model\BrokerMobile;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BrokerMobileController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('手机列表')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BrokerMobile);
        $grid->mobile('手机号');
        $grid->is_bind('是否绑定')->display(function($value){
            return $value==1 ? '已绑定' : '未绑定';
        });
        $grid->actions(function(Grid\Displayers\Actions $actions){
            $actions->disableEdit();
            $actions->disableView();
        });
        $grid->filter(function(Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->equal('mobile','手机号');
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BrokerMobile::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BrokerMobile);

        $form->text('mobile','手机号')->rules('required|unique:broker_mobile,mobile',[
            'required'=>'请填写手机号',
            'unique'=>'手机号重复'
        ]);

        return $form;
    }
    public function destroy($id)
    {
        $brokerMobile= BrokerMobile::find($id);

       $brokerCall= Broker::where('bind_mobile',$brokerMobile->mobile)->first();
       if($brokerCall){
           $data = [
               'status'  => false,
               'message' => '手机号已绑定，请先删除绑定关系',
           ];
           return response()->json($data);
       }
        if ($this->form()->destroy($id)) {
            $data = [
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ];
        } else {
            $data = [
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ];
        }

        return response()->json($data);
    }
}
